package com.tianyao.compose.baseuse.activity.animate

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.Animatable
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.tianyao.compose.baseuse.theme.ComposeProTheme

/**
 * 动画的低级别api的Activity
 */
class AnimatedLowGradeApi2Activity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeProTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.White)
                ) {
                    Greeting()
                }
            }
        }
    }


    @Composable
    fun Greeting() {
        //TestAnimateColorAsState()
        TestAnimatable()
    }


    @Preview
    @Composable
    fun TestAnimateColorAsState() {
        var isLike by remember { mutableStateOf(false) }
        val buttonSize by animateDpAsState(
            targetValue = if (isLike) 32.dp else 24.dp,
            animationSpec = tween(3000),
            label = ""
        )
        val buttonColor by animateColorAsState(
            targetValue = if (isLike) Color.Red else Color.Gray,
            animationSpec = tween(3000), label = ""
        )

        IconButton(onClick = {
            isLike = !isLike
        }) {
            Icon(
                imageVector = Icons.Rounded.Favorite,
                contentDescription = null,
                modifier = Modifier.size(buttonSize),
                tint = buttonColor
            )

        }
    }


    @Preview
    @Composable
    fun TestAnimatable() {
        var isLike by remember { mutableStateOf(false) }
        //Animatable修改尺寸
        val buttonSize = remember { Animatable(24.dp, Dp.VectorConverter) }
        //Animatable修改颜色
        val buttonColor = remember { Animatable(Color.Gray) }

        LaunchedEffect(isLike) {
            //animateTo
            buttonColor.animateTo(
                targetValue = if (isLike) Color.Red else Color.Gray,
                animationSpec = tween(2000)
            )
            //animateTo
            buttonSize.animateTo(
                targetValue = if (isLike) 48.dp else 24.dp,
                animationSpec = tween(2000)
            )
        }

        IconButton(onClick = {
            isLike = !isLike
        }) {
            Icon(
                imageVector = Icons.Rounded.Favorite,
                contentDescription = null,
                modifier = Modifier.size(buttonSize.value),
                tint = buttonColor.value
            )
        }
    }

}
